<?php

include '../../libraries/dbconfig.php';
include '../../libraries/messages.php';
session_start();
date_default_timezone_set('Asia/Calcutta');
$date1 = date("Y-m-d H:i:s");
$productid = $_POST['productid'];
$qty = $_POST['qty'];
$discount = $_POST['discount'];
//If customer selected
if ($_SESSION['customer']) {
$stock = mysql_query("select qty,free,sold from invoicedetails where productid = $productid");
$stock_data = mysql_fetch_array($stock);
$balance = ($stock_data['qty'] + $stock_data['free']) - $stock_data['sold'];


if ($balance >= $qty) {

    $dsc = mysql_query("select * from options");
    $dsc_data = mysql_fetch_array($dsc);

    $products = mysql_query("select * from products where productid = $productid");

    $products_data = mysql_fetch_array($products);
    $product_name = $products_data['name'];

    if ($dsc_data['discount_type'] == "amount") {
        $discount_product = $discount;
        $discount_product_total = $discount * $qty;
        $price = $products_data['price'];
        $vat = ((($products_data['price'] - $discount_product) / 100) * 14.5) * $qty;
        $amount = (($price - $discount_product) * $qty) + $vat;
    } else {
        //discount = ($products_data['price'] / 100) * $discount;
        $discount_product = (($products_data['price'] / 100) * $discount);
        $discount_product_total = (($products_data['price'] / 100) * $discount) * $qty;
        $price = $products_data['price'];
        $vat = ((($price - $discount_product) / 100) * 14.5) * $qty;
        $amount = (($price - $discount_product) * $qty) + $vat;
    }

    if ($_SESSION['display']) {


        $display = $_SESSION['display'];
        if (in_array( "'productid' => $productid", $display)) {
    echo "Got Irix";
}
        array_push($display, array("productid" => $productid, "name" => $product_name, "qty" => $qty, "discount" => $discount, "vat" => $vat, "price" => $price, "discount_product_total" => $discount_product_total, "amount" => $amount));
        $_SESSION['display'] = $display;
    } else {

        $display = array();
        //$display[0] = $_POST;

        $display[0] = array("productid" => $productid, "name" => $product_name, "qty" => $qty, "discount" => $discount, "vat" => $vat, "price" => $price, "discount_product_total" => $discount_product_total, "amount" => $amount);

        $_SESSION['display'] = $display;
    }


    $result = $display;

    for ($row = 0; $row < count($result); $row++) {
        // echo "  Name :" . $result[$row]['productid'] . " <br> qty :" . $result[$row]['qty'];
        // echo "<br />";

        $product_id = $result[$row]['productid'];
        $discount = $result[$row]['discount'];

        $products = mysql_query("select * from products where productid = $product_id");
        $products_data = mysql_fetch_array($products);

        if ($dsc_data['discount_type'] == "amount") {
            $total = ($products_data['price'] - $discount) * $result[$row]['qty'];
            $dup_total = $total_rate + $total;
            $total_rate = $dup_total;
            $vat_tax = ($total / 100) * 14.5;
            $amount = $total + $vat_tax;
            $dup_vat = $total_vat + $vat_tax;
            $total_vat = $dup_vat;
            $bill_total = $total_rate + $total_vat;
            $dup_discount = $total_discount + ($discount * $result[$row]['qty']);
            $total_discount = $dup_discount;
        } else {
            $total = ($products_data['price'] - ((($products_data['price']) / 100) * $discount)) * $result[$row]['qty'];
            $dup_total = $total_rate + $total;
            $total_rate = $dup_total;
            $vat_tax = ($total / 100) * 14.5;
            $amount = $total + $vat_tax;
            $dup_vat = $total_vat + $vat_tax;
            $total_vat = $dup_vat;
            $bill_total = $total_rate + $total_vat;
            $dup_discount = $total_discount + (((($products_data['price']) / 100) * $discount) * $result[$row]['qty']);
            $total_discount = $dup_discount;
        }
    }













    $total = array();
    $total["date"] = $date1;
    $total["total_rate"] = $total_rate;
    $total["total_discount"] = $total_discount;
    $total["total_vat"] = $total_vat;
    $total["bill_total"] = $bill_total;
    $notice = array();
    $notice[mesg] = "";

    $back = array($display, $total, $notice);
    $_SESSION['total'] = $total;

    echo json_encode($back);
} else {
    $display = $_SESSION['display'];
    $total = $_SESSION['total'];
    $notice = array();
    $notice[mesg] = "<p style='color:red'>Stock not available as per your requirement (Available = $balance)</p>";

    $back = array($display, $total, $notice);

    echo json_encode($back);
}
} else {
        $notice = array();
    $notice[mesg] = "<p style='color:red'>$message[16]</p>";

    $back = array($display, $total, $notice);

    echo json_encode($back);
    //echo $message[16];
}
?>
   